$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/expertManage/list',
        datatype: "json",
        colModel: [
            { label: '头像', name: 'circleHeadUrl',sortable:false, width: 80, formatter: function(value, options, row){

                return   value==undefined || value==null || value=="" ? '<image src="/statics/img/noImage.png" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>' : '<image src="'+value+'" style="width: 50%; height: auto;max-width: 50%; display: block;"></image>';
            }},
			{ label: '用户ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true},
			{ label: '姓名', name: 'name', width: 75 },
			{ label: '性别', name: 'sex', width: 35,formatter: function(value, options, row){
                return value === 0 ?
                    '男' :
                    '女';
            }},
            { label: '医学职称', name: 'medicalTitle', width: 100 },
            { label: '出诊', name: 'surgery', width: 120 },
			{ label: '更新时间', name: 'updateTime', width: 100,sortable:false}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});

function uploadImage(id,name)
{


    $(id).fileupload({
        url: baseURL + "fileUpload/image",
        type: "POST",
        autoUpload: true,
        /*formData:{desc:$("#ed_desc").val()},*/
        sequentialUploads: true
    }).bind('fileuploadprogress', function (e, data) {
        //$("#down_div").removeClass("xhide");
        // $("#over_div").removeClass("xhide");

        if(name=="headUrl")
            vm.headUrl = data.originalFiles[0].name;
        else if(name=="qrCodeUrl")
            vm.qrCodeUrl = data.originalFiles[0].name;
        else if(name=="circleHeadUrl")
            vm.circleHeadUrl = data.originalFiles[0].name;
        else if(name=="contactHeadUrl")
            vm.contactHeadUrl = data.originalFiles[0].name;

    }).bind('fileuploaddone', function (e, data) {
        // $("#down_div").addClass('xhide');
        // $("#over_div").addClass('xhide');
        if(data.result.errcode==0){
            //$("#upload").val(data.result.data);

			if(name=="headUrl")
               vm.expert.headUrl = data.result.data;
			else if(name=="qrCodeUrl")
                vm.expert.qrCodeUrl = data.result.data;
            else if(name=="circleHeadUrl")
                vm.expert.circleHeadUrl = data.result.data;
            else if(name=="contactHeadUrl")
                vm.expert.contactHeadUrl = data.result.data;

            alert("上传成功！");
        }else{
            alert("上传失败！");
        }

    });


}


var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name: null
		},
        requestUrl:baseURL+"fileUpload/image",
        token:{token:localStorage.getItem("token")},
        headUrl:"",
        qrCodeUrl:"",
        circleHeadUrl:"",
        contactHeadUrl:"",
		showList: true,
		title:null,
		expert:{
			status:0
		}
	},
	methods: {
        handlePreview0(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess0(res, file) {
            vm.headUrl=res.data;
        },
        handlePreview1(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess1(res, file) {
            vm.circleHeadUrl=res.data;
        },
        handlePreview2(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess2(res, file) {
            vm.contactHeadUrl=res.data;
        },
        handlePreview3(file) {
            const isJPG = file.type === 'image/jpeg' || file.type === 'image/png';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
                this.$message.error('上传头像图片只能是 JPG或者PNG 格式!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        handleAvatarSuccess3(res, file) {
            vm.qrCodeUrl=res.data;
        },
		query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.expert = {status:0,sex:"0"};
            vm.headUrl="";
            vm.qrCodeUrl="";
            vm.circleHeadUrl="";
            vm.contactHeadUrl="";
		},
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
			
			vm.getUser(userId);

		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "orth/expertManage/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {

            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            /*if(vm.validator()){
                return ;
            }*/


                if(vm.headUrl==null)
                {
                    alert("请上传方形头像");
                    return;
                }
                else if(vm.qrCodeUrl==null)
                {
                    alert("请上传二维码");
                    return;
                }
                else if(vm.circleHeadUrl==null)
                {
                    alert("请上传圆形头像");
                    return;
                }
                else if(vm.contactHeadUrl==null)
                {
                    alert("请上传联系我们头像");
                    return;
                }
                vm.expert.circleHeadUrl=vm.circleHeadUrl;
                vm.expert.contactHeadUrl=vm.contactHeadUrl;
                vm.expert.headUrl=vm.headUrl;
                vm.expert.qrCodeUrl=vm.qrCodeUrl;


			var url = vm.expert.id_ == null ? "orth/expertManage/save" : "orth/expertManage/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.expert),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getUser: function(id){
			$.get(baseURL + "orth/expertManage/info/"+id, function(r){
				vm.expert = r.expert;
				vm.qrCodeUrl=vm.expert.qrCodeUrl;
				vm.headUrl=vm.expert.headUrl;
				vm.contactHeadUrl=vm.expert.contactHeadUrl;
				vm.circleHeadUrl=vm.expert.circleHeadUrl;
			});
		},
		reload: function () {
			vm.showList = true;
            $(".formError").hide();
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		},
        validator: function () {

        }
	}
});